package com.hy.item.web;


import com.hy.common.dto.CartDTo;
import com.hy.common.vo.PageResult;
import com.hy.item.pojo.Sku;
import com.hy.item.pojo.Spu;
import com.hy.item.pojo.SpuDetail;
import com.hy.item.service.GoosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class GoosController {
    @Autowired
    private GoosService goosService;

    /**
     * 分页查询商品列表
     */
    @RequestMapping("spu/page")
    public ResponseEntity<PageResult<Spu>> querySpuByPage(

            @RequestParam(value = "page",defaultValue = "1") Integer page,
            @RequestParam(value = "rows",defaultValue = "5") Integer rows,
            @RequestParam(value = "saleable",required = false) Boolean saleable,
            @RequestParam(value = "key",required = false) String key

           )
    {
        PageResult<Spu> spuPageResult = goosService.querySpuByPage(page, rows, saleable, key);
        return ResponseEntity.ok(spuPageResult) ;

    }

    /**
     * 新增商品
     * @param spu
     * @return
     */
    @RequestMapping("spu/goods")
    public ResponseEntity<Void> saveGoods(@RequestBody Spu spu){
        goosService.saveGoods(spu);
        return ResponseEntity.status(HttpStatus.CREATED).build();

    }

    /**
     * 根据spuId查询tb_spu_detail表
     * @param spuId
     * @return
     */
    @GetMapping("spu/detail/{id}")
    public ResponseEntity<SpuDetail> queryDetailById(@PathVariable("id") Long spuId){

        return ResponseEntity.ok(goosService.queryDetailById(spuId));

    }

    /**
     * 根据spuId查询下面的所有sku
     * @param id
     * @return
     */
    @GetMapping("sku/list")
    public ResponseEntity<List<Sku>> querySkuById(@RequestParam("id") Long id){

        return ResponseEntity.ok(goosService.querySkuById(id));
    }

    /**
     * 根据sku的id集合查询所有的sku
     * @param ids
     * @return
     */
    @GetMapping("sku/list/ids")
    public ResponseEntity<List<Sku>> querySkuByIds(@RequestParam("ids") List<Long> ids){

        return ResponseEntity.ok(goosService.querySkuByIds(ids));
    }

    /**
     * 根据spu的id查询spu
     * @param id
     * @return
     */
    @GetMapping("spu/{id}")
    public ResponseEntity<Spu> querySpuById(@PathVariable("id") Long id){

        return ResponseEntity.ok(goosService.querySpuById(id));

    }

    /**
     * 减库存
     * @param carts
     * @return
     */
    @PostMapping("stock/decrease")
    public ResponseEntity<Void> decreaseStock(@RequestBody List<CartDTo> carts){

        goosService.decreaseStock(carts);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();

    }
}
